﻿using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CuaHangLego.UI
{
    public partial class HoaDonMuaDetail : Form
    {
        private HoaDonMuaBSS hdmss;
        private SanPhamKhoBSS spbss;
        private HoaDonMua hoadon;

        public HoaDonMuaDetail(SanPhamKhoBSS spbss, HoaDonMuaBSS bss, HoaDonMua hoadon)
        {
            InitializeComponent();
            tblSPHDM.AutoGenerateColumns = false;
            tblCTHDM.AutoGenerateColumns = false;
            this.hdmss = bss;
            this.spbss = spbss;
            this.hoadon = hoadon;
        }

        private void buttonThemCTHDMua_Click(object sender, EventArgs e)
        {

            if (tblSPHDM.SelectedRows.Count > 0)
            {
                ShowSoLuongUI();
            }
            
        }

        private void BtThemHDMua_Click(object sender, EventArgs e)
        {
            try
            {
                this.hoadon.ThoiGian = txtNgayHDM.Value;
                this.hoadon.NhaCungCap = cbxNCC.SelectedItem as NhaCungCap;
                this.hdmss.Add(this.hoadon);
            }
            catch
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
            }
            finally
            {
                this.hdmss.Commit();
                MessageBox.Show("Đã thêm hóa đơn thành công", "Thông báo thêm hóa đơn");
                this.hoadon = new HoaDonMua();
                RefreshUI();
            }
        }

        private void btSuaHDMua_Click(object sender, EventArgs e)
        {
            try
            {
                this.hoadon.ThoiGian = txtNgayHDM.Value;
                this.hoadon.NhaCungCap = cbxNCC.SelectedItem as NhaCungCap;
                this.hdmss.Add(this.hoadon);
            }
            catch
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
            }
            finally
            {
                this.hoadon.StateType = BaseModel.StateTypes.WillBeChange;
                this.hdmss.Commit();
                MessageBox.Show("Sửa thành công hóa đơn!");
                RefreshUI();
            }
        }

        private void ThemHDMuaUI_Load(object sender, EventArgs e)
        {
            spbss.UpdateData();
            tblSPHDM.DataSource = spbss.GetAll();
            cbxNCC.DataSource = NhaCungCapDA.Instance.GetAll();
            if (this.hoadon != null)
            {
                cbxNCC.SelectedIndex = cbxNCC.FindStringExact(hoadon.NhaCungCap.Ten);
                txtNgayHDM.Value = hoadon.ThoiGian;
                tblCTHDM.DataSource = null;
                tblCTHDM.DataSource = this.hoadon.DsCTHD;
                buttonSuaHDMua.Enabled = true;
                buttonThemHDMua.Enabled = false;
            }
            else
            {
                this.hoadon = new HoaDonMua();
                buttonSuaHDMua.Enabled = false;
                buttonThemHDMua.Enabled = true;
            }
        }

        private void RefreshUI()
        {
            tblCTHDM.DataSource = null;
            tblCTHDM.DataSource = this.hoadon.DsCTHD;
        }

        private void buttonRemoveFromCTHDM_Click(object sender, EventArgs e)
        {
            if (tblCTHDM.SelectedRows.Count > 0)
            {
                this.hoadon.DsCTHD.Remove(tblCTHDM.SelectedRows[0].DataBoundItem as CTHD);
                RefreshUI();
            }
        }

        private void buttonThemSP_Click(object sender, EventArgs e)
        {
            SanPhamDetail f = new SanPhamDetail(spbss, null);
            f.StartPosition = FormStartPosition.CenterParent;
            f.ShowDialog();
            spbss.Commit();
            spbss.UpdateData();
            tblSPHDM.DataSource = spbss.GetAll();
        }

        private void tblSPHDM_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowSoLuongUI();
        }

        private void ShowSoLuongUI()
        {
            CTHD cthd = new CTHD();
            cthd.StateType = BaseModel.StateTypes.WillBeAdd;
            cthd.SanPham = tblSPHDM.SelectedRows[0].DataBoundItem as SanPhamKho;
            SoLuongUI frm = new SoLuongUI();
            frm.StartPosition = FormStartPosition.CenterParent;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                cthd.SoLuong = frm.SoLuong;
                bool isHas = false;
                foreach (CTHD ct in this.hoadon.DsCTHD)
                {
                    if (ct.SanPham.MaSo == cthd.SanPham.MaSo)
                    {
                        isHas = true;
                        ct.SoLuong += cthd.SoLuong;
                        break;
                    }
                }

                if (!isHas) this.hoadon.DsCTHD.Add(cthd);

                RefreshUI();
            }
        }

        private void buttonSearchHDM_Click(object sender, EventArgs e)
        {
            tblSPHDM.DataSource = spbss.GetSanPhamBy(txtKeywordHDM.Text);
        }

        private void buttonAllHDM_Click(object sender, EventArgs e)
        {
            tblSPHDM.DataSource = spbss.GetAll();
        }
    }
}
